package com.huizu.project.controller;

import java.rmi.ServerException;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.huizu.common.core.domain.entity.SysUser;
import com.huizu.common.exception.ServiceException;
import com.huizu.common.utils.SecurityUtils;
import com.huizu.project.domain.SysUserAuth;
import com.huizu.project.service.ISysUserAuthService;
import com.huizu.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.huizu.common.annotation.Log;
import com.huizu.common.core.controller.BaseController;
import com.huizu.common.core.domain.AjaxResult;
import com.huizu.common.enums.BusinessType;
import com.huizu.project.domain.ProBj;
import com.huizu.project.service.IProBjService;
import com.huizu.common.utils.poi.ExcelUtil;
import com.huizu.common.core.page.TableDataInfo;

/**
 * 班级管理Controller
 * 
 * @author zhangmx
 * @date 2024-05-11
 */
@RestController
@RequestMapping("/project/probj")
public class ProBjController extends BaseController
{
    @Autowired
    private IProBjService proBjService;

    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private ISysUserAuthService iSysUserAuthService;

    /**
     * 查询班级管理列表
     */
    //@PreAuthorize("@ss.hasPermi('project:probj:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProBj proBj) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (!SecurityUtils.isAdmin(user.getUserId())) {
            SysUserAuth sysUserAuthPram = new SysUserAuth();
            sysUserAuthPram.setUserId(user.getUserId());
            List<SysUserAuth> auths = iSysUserAuthService.selectSysUserAuthList(sysUserAuthPram);
            List<String> zyIds = auths.stream().filter(auth -> auth.getType().equals("bj")).map(SysUserAuth::getBusId).collect(Collectors.toList());
            proBj.setBjIds(zyIds);
        }
        startPage();
        List<ProBj> list = proBjService.selectProBjList(proBj);
        return getDataTable(list);
    }


    /**
     * 查询班级管理列表
     */
    //@PreAuthorize("@ss.hasPermi('project:probj:list')")
    @GetMapping("/listTotal")
    public TableDataInfo listTotal(ProBj proBj)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (!SecurityUtils.isAdmin(user.getUserId())) {
            SysUserAuth sysUserAuthPram = new SysUserAuth();
            sysUserAuthPram.setUserId(user.getUserId());
            List<SysUserAuth> auths = iSysUserAuthService.selectSysUserAuthList(sysUserAuthPram);
            List<String> zyIds = auths.stream().filter(auth -> auth.getType().equals("bj")).map(SysUserAuth::getBusId).collect(Collectors.toList());
            proBj.setBjIds(zyIds);
        }
        List<ProBj> list = proBjService.selectProBjList(proBj);
        return getDataTable(list);
    }

    /**
     * 导出班级管理列表
     */
    @PreAuthorize("@ss.hasPermi('project:probj:export')")
    @Log(title = "班级管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProBj proBj)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (!SecurityUtils.isAdmin(user.getUserId())) {
            SysUserAuth sysUserAuthPram = new SysUserAuth();
            sysUserAuthPram.setUserId(user.getUserId());
            List<SysUserAuth> auths = iSysUserAuthService.selectSysUserAuthList(sysUserAuthPram);
            List<String> zyIds = auths.stream().filter(auth -> auth.getType().equals("bj")).map(SysUserAuth::getBusId).collect(Collectors.toList());
            proBj.setBjIds(zyIds);
        }
        List<ProBj> list = proBjService.selectProBjList(proBj);
        ExcelUtil<ProBj> util = new ExcelUtil<ProBj>(ProBj.class);
        util.exportExcel(response, list, "班级管理数据");
    }

    /**
     * 获取班级管理详细信息
     */
    //@PreAuthorize("@ss.hasPermi('project:probj:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        ProBj bj = proBjService.selectProBjById(id);
        return success(bj);
    }

    /**
     * 新增班级管理
     */
    @PreAuthorize("@ss.hasPermi('project:probj:add')")
    @Log(title = "班级管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProBj proBj) {
        ProBj bj = new ProBj();
        bj.setNumber(proBj.getNumber());
        List<ProBj> list = proBjService.selectProBjList(bj);
        if (CollUtil.isNotEmpty(list)){
            throw new ServiceException("对不起，您输入的班级编号重复[" +proBj.getNumber() + "]");
        }
        return toAjax(proBjService.insertProBj(proBj));
    }

    /**
     * 修改班级管理
     */
    @PreAuthorize("@ss.hasPermi('project:probj:edit')")
    @Log(title = "班级管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProBj proBj)
    {
        return toAjax(proBjService.updateProBj(proBj));
    }

    /**
     * 删除班级管理
     */
    @PreAuthorize("@ss.hasPermi('project:probj:remove')")
    @Log(title = "班级管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(proBjService.deleteProBjByIds(ids));
    }
}
